package com.feiyi.app.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.feiyi.app.R;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseActivity;
import com.feiyi.app.bean.CommonBean;
import com.feiyi.app.net.HttpMethods;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * @author wangliantao
 * @time 2018 11 2018/11/18 8:19 PM
 * @email 1346947404@qq.com
 * @package com.feiyi.app.activity
 * @describe： In me the tiger sniffs the rose
 */
public class ForgetPasswordActivity extends BaseActivity {
    Unbinder unbinder;
    @BindView(R.id.et_username)
    EditText et_username;

    @BindView(R.id.et_psd)
    EditText et_psd;
    @BindView(R.id.et_yanzhegnma)
    EditText et_yanzhengma;
    @BindView(R.id.tv_send_yangzhengma)
    TextView tv_send_yanzhengma;

    private String userName;
    private String password;
    private String yanzhengma;

    public int total = 60000;

    private int current = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpsd);
        unbinder = ButterKnife.bind(this);

    }
    private void getEditString(){
        userName = et_username.getText().toString();

        password = et_psd.getText().toString();
        yanzhengma = et_yanzhengma.getText().toString();

    }

    private void doGetPsd(){
        getEditString();
        if (null == userName || userName.isEmpty()){
            ToastUtils.showShort("用户名不能为空");
            return;
        }
        if (null == yanzhengma || yanzhengma.isEmpty()){
            ToastUtils.showShort("验证码不能为空");
            return;
        }

        if (null == password || password.isEmpty()){
            ToastUtils.showShort("密码不能为空");
            return;
        }
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("name",userName);

        builder.add("password",password);
        builder.add("code",yanzhengma);
        RequestBody body = builder.build();
        HttpMethods.getInstance().info.getpsd(AppConstant.GETPSD_URL,body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(CommonBean registerBean) {
                        if (registerBean.getStatus().getSucceed() == 1){//注册成功
                            ToastUtils.showShort("找回密码成功");
                            finish();
                        }else {//注册失败
                            ToastUtils.showShort("找回密码失败"+registerBean.getStatus().getError_desc());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @OnClick({R.id.iv_back_titlebar,R.id.but_getpsd,R.id.tv_send_yangzhengma})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back_titlebar:
                finish();
                break;
            case R.id.but_getpsd:
                doGetPsd();
                break;
            case R.id.tv_send_yangzhengma:
                sendYanzhengma();
                break;
        }
    }
    private void sendYanzhengma(){
        getEditString();
        if (null == userName || userName.isEmpty()){
            ToastUtils.showShort("用户名不能为空");
            return;
        }
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("name",userName);

        RequestBody body = builder.build();
        HttpMethods.getInstance().info.sendYanzhengma(AppConstant.YANZHENGMA_GETPSD_URL,body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(CommonBean registerBean) {
                        if (registerBean.getStatus().getSucceed() == 1){//注册成功
                            ToastUtils.showShort("验证码发送成功");
                            tv_send_yanzhengma.setTextColor(getResources().getColor(R.color.white));
                            tv_send_yanzhengma.setBackground(getResources().getDrawable(R.drawable.shape_btn_yanzhengma));
                            handler.sendEmptyMessage(0);
                        }else {//注册失败
                            ToastUtils.showShort("验证码发送失败"+registerBean.getStatus().getError_desc());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    if (current < total){
                        current+=1000;
                        tv_send_yanzhengma.setText((60-current/1000)+"S");
                        handler.sendEmptyMessageDelayed(0,1000);
                    }else {
                        current = 0;
                        tv_send_yanzhengma.setBackground(null);
                        tv_send_yanzhengma.setTextColor(getResources().getColor(R.color.colorPrimary));
                        tv_send_yanzhengma.setText("获取验证码");

                    }
                    break;

            }
        }
    };
}
